package com.hz.ironc.cloud.thrift.server.conf;

import com.hz.ironc.cloud.thrift.server.service.UserServiceImpl;
import com.hz.ironc.cloud.thritf.api.UserService;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by tfw2302 on 2017/1/19.
 */
@Configuration
public class ThriftConfig {

    ExecutorService executorService = Executors.newSingleThreadExecutor();

    @Bean
    public TServerTransport tServerTransport(){
        try {
            return new TServerSocket(7911);
        } catch (TTransportException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Bean
    public TServer tServer(){
        // 发布服务
        UserService.Processor processor = new UserService.Processor(new UserServiceImpl());
        TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(tServerTransport()).processor(processor));
        return server;
    }

    @PostConstruct
    public void init(){
        executorService.execute(() -> tServer().serve());
    }

}
